﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HelperLibrary.cs" company="None">
//   <Author>Adrian von Allmen, 2013</Author>
// </copyright>
// <summary>
//   Main Libary with several important features
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Resources;

namespace CrumbleLibrary
{
    public static class HelperLibrary
    {
        #region Enums

        /// <summary>
        /// A list for allowed dynamic types. Has to be WPF Controls.
        /// </summary>
        /// If you implement new Enums, you'll have to add them within the dynamic filter page as well
        public enum FilterType
        {
            /// <summary>
            /// WPF TextBox.
            /// </summary>
            Textbox,

            /// <summary>
            /// WPF Checkbox.
            /// </summary>
            CheckBox,

            /// <summary>
            /// WPF ListBox.
            /// </summary>
            ListBox,

            /// <summary>
            /// WPF ComboBox.
            /// </summary>
            ComboBox,

        }

        /// <summary>
        /// The report type.
        /// </summary>
        public enum ReportType
        {
            /// <summary>
            /// Microsoft Reporting Services (Local)
            /// </summary>
            MSRS,

            /// <summary>
            /// Telerik Reporting (Future Roadmap)
            /// </summary>
            Telerik,

            /// <summary>
            /// Infragistics NetAdvantage Reporting (Future Roadmap)
            /// </summary>
            Infragistics,

            /// <summary>
            /// Combit List & Labels (Future Roadmap)
            /// </summary>
            Combit
        }

        /// <summary>
        /// The filter parameter type.
        /// </summary>
        public enum FilterParameterType
        {
            /// <summary>
            /// A report parameter.
            /// </summary>
            Report,

            /// <summary>
            /// A sql Parameter. Currently only MS SQL supported, so naming contain MS
            /// </summary>
            MSSQL,

            /// <summary>
            /// A string for example as document text.
            /// </summary>
            String,

            /// <summary>
            /// Paramater for default value filter.
            /// </summary>
            FilterControl,

            /// <summary>
            /// Paramater for dynamic filters.
            /// </summary>
            FilterDynamic,

            /// <summary>
            /// Paramater for filter restrictions.
            /// </summary>
            FilterVisibility,

            /// <summary>
            /// Parameter for filter settings
            /// </summary>
            FilterSetting
        }
        #endregion

        public static ResourceManager GetDefaultTranslationFile(string fileName = @"\Translation.dll")
        {
            string defaultFile = Directory.GetCurrentDirectory() + fileName;

            if (File.Exists(defaultFile))
            {
                var translationAssembly = Assembly.LoadFrom(defaultFile);

                var manifests = translationAssembly.GetManifestResourceNames();
                if (manifests.Length == 1)
                {
                    string manifest = manifests[0].Replace(".resources", string.Empty);
                    return new ResourceManager(manifest, translationAssembly);
                }
            }

            return null;
        }

        /// <summary>
        /// Retrieve a MSSQL DataType
        /// </summary>
        /// <param name="theType"> .Net Type of Value </param>
        /// <returns>
        /// Return the linkes MSSQL datatype of .Net datatype 
        /// </returns>
        public static SqlDbType GetMssqldbType(Type theType)
        {
            var param = new SqlParameter();
            TypeConverter tc = TypeDescriptor.GetConverter(param.DbType);
            if (tc.CanConvertFrom(theType))
            {
                var convertFrom = tc.ConvertFrom(theType.Name);
                if (convertFrom != null)
                {
                    param.DbType = (DbType)convertFrom;
                }
            }
            else
            {
                // try to forcefully convert
                try
                {
                    var convertFrom = tc.ConvertFrom(theType.Name);
                    if (convertFrom != null)
                    {
                        param.DbType = (DbType)convertFrom;
                    }
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch
                // ReSharper restore EmptyGeneralCatchClause
                {
                    // ignore the exception
                }
            }
            return param.SqlDbType;
        }

        /// <summary>
        /// Retrieve a SQL Data Type
        /// </summary>
        /// <param name="theType"> .Net Type of Value </param>
        /// <returns> Return the linked SQL data type of .Net data type </returns>
        public static SqlDbType GetMsSqlDbType(Type theType)
        {
            var param = new SqlParameter();
            TypeConverter tc = TypeDescriptor.GetConverter(param.DbType);
            if (tc.CanConvertFrom(theType))
            {
                var convertFrom = tc.ConvertFrom(theType.Name);
                if (convertFrom != null)
                {
                    param.DbType = (DbType)convertFrom;
                }
            }
            else
            {
                // try to forcefully convert
                try
                {
                    var convertFrom = tc.ConvertFrom(theType.Name);
                    if (convertFrom != null)
                    {
                        param.DbType = (DbType)convertFrom;
                    }
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch
                // ReSharper restore EmptyGeneralCatchClause
                {
                    // ignore the exception
                }
            }

            return param.SqlDbType;
        }
    }
}
